package controller

import (
	"system-admin/internal/app/web/service/user"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Index struct{}

// Login 登录
func (Index) Login(c *gin.Context) {
	var params struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}
	c.ShouldBindJSON(&params)
	userID, err := user.LoginForDevice(params.Username, params.Password)
	response.JSON(c, gin.H{
		"user_id": userID,
	}, err)
}

// LoginForDevice 设备端登录
func (Index) LoginForDevice(c *gin.Context) {
	var params struct {
		Username string `json:"username"`
		Password string `json:"password"`
	}
	c.ShouldBindJSON(&params)
	userID, err := user.LoginForDevice(params.Username, params.Password)
	response.JSON(c, gin.H{
		"user_id": userID,
	}, err)
}

// GetCaptcha 获取图形验证码
func (Index) GetCaptcha(c *gin.Context) {
	captcha, err := user.GetCaptcha()
	response.JSON(c, gin.H{
		"captcha_id":  captcha.CaptchaID,
		"captcha_img": captcha.CaptchaImg,
	}, err)
}

// LoginV2 登录v2版本
func (Index) LoginV2(c *gin.Context) {
	var params struct {
		Username    string `json:"username"`
		Password    string `json:"password"`
		CaptchaID   string `json:"captcha_id"`
		CaptchaCode string `json:"captcha_code"`
	}
	c.ShouldBindJSON(&params)
	token, err := user.LoginV2(params.Username, params.Password, params.CaptchaID, params.CaptchaCode)
	response.JSON(c, gin.H{
		"token": token,
	}, err)
}
